package cn.csc.module.shop.service.audit;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.member.api.user.MemberUserApi;
import cn.csc.module.member.api.user.dto.UpdateUserTypeReqDTO;
import cn.csc.module.shop.controller.admin.audit.vo.ShopAuditCreateReqVO;
import cn.csc.module.shop.controller.admin.audit.vo.ShopAuditExportReqVO;
import cn.csc.module.shop.controller.admin.audit.vo.ShopAuditPageReqVO;
import cn.csc.module.shop.controller.admin.audit.vo.ShopAuditUpdateReqVO;
import cn.csc.module.shop.convert.audit.ShopAuditConvert;
import cn.csc.module.shop.dal.dataobject.audit.ShopAuditDO;
import cn.csc.module.shop.dal.dataobject.user.ShopUserDO;
import cn.csc.module.shop.dal.mysql.audit.ShopAuditMapper;
import cn.csc.module.shop.dal.mysql.detail.ShopDetailMapper;
import cn.csc.module.shop.dal.mysql.user.ShopUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shop.enums.ErrorCodeConstants.SHOP_AUDIT_NOT_EXISTS;

/**
 * 店铺审核记录 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class ShopAuditServiceImpl extends ServiceImpl<ShopAuditMapper, ShopAuditDO> implements ShopAuditService {

    @Resource
    private ShopDetailMapper shopDetailMapper;

    @Resource
    private ShopUserMapper shopUserMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public BigInteger createAudit(ShopAuditCreateReqVO createReqVO) {
        // 插入
        ShopAuditDO audit = ShopAuditConvert.INSTANCE.convert(createReqVO);
        baseMapper.insert(audit);
        shopDetailMapper.updateAuditStatus(createReqVO.getShopId(), createReqVO.getAuditStatus(), createReqVO.getAuditType());
        // 审核通过 关联角色, 设置用户类型
        if (createReqVO.getAuditStatus() == 2 && (createReqVO.getType() == 5 || createReqVO.getType() == 10 || createReqVO.getType() == 15)){
            ShopUserDO shopUserDO = shopUserMapper.selectByShopId(createReqVO.getShopId());
            memberUserApi.updateUserType(new UpdateUserTypeReqDTO(shopUserDO.getUserId(), createReqVO.getAuditType()));
        }
        // 返回
        return audit.getId();
    }

    @Override
    public void updateAudit(ShopAuditUpdateReqVO updateReqVO) {
        // 校验存在
        validateAuditExists(updateReqVO.getId());
        // 更新
        ShopAuditDO updateObj = ShopAuditConvert.INSTANCE.convert(updateReqVO);
        baseMapper.updateById(updateObj);
    }

    @Override
    public void deleteAudit(BigInteger id) {
        // 校验存在
        validateAuditExists(id);
        // 删除
        baseMapper.deleteById(id);
    }

    private void validateAuditExists(BigInteger id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(SHOP_AUDIT_NOT_EXISTS);
        }
    }

    @Override
    public ShopAuditDO getAudit(BigInteger id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<ShopAuditDO> getAuditList(Collection<BigInteger> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopAuditDO> getAuditListByShopId(BigInteger shopId) {
        return baseMapper.selectListByShopId(shopId);
    }

    @Override
    public PageResult<ShopAuditDO> getAuditPage(ShopAuditPageReqVO pageReqVO) {
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopAuditDO> getAuditList(ShopAuditExportReqVO exportReqVO) {
        return baseMapper.selectList(exportReqVO);
    }

    @Override
    public ShopAuditDO getLatestByShopId(BigInteger shopId){
        return baseMapper.selectLatestByShopId(shopId);
    }


}
